package it.CookBook;

import java.util.ArrayList;

import android.graphics.Paint;

public class StringUtil {
	
	public static ArrayList<String> splitStrint(String strIn, Paint pntToMeasure, int maxSize){
		ArrayList<String> result = new ArrayList<String>();
		pntToMeasure.measureText(strIn);
		String[] splittedString = strIn.split(" ");
		String temp = new String();
		boolean alreadyAdd = false;
		for (int i = 0; i < splittedString.length; i++) {
			if (pntToMeasure.measureText(temp + " " + splittedString[i]) > maxSize){
				result.add(temp);
				temp = splittedString[i];
				alreadyAdd = true;
			}
			else
			{
				temp +=  " " + splittedString[i];
				alreadyAdd = false;
			}
			if (i + 1 == splittedString.length && !alreadyAdd)
				result.add(temp);
		}
		return result;
	}
	
	public static String measureAndSplit(String strIn, int start, int end){
		int lastSpace = strIn.lastIndexOf(strIn.subSequence(start, end).toString());
		return strIn.subSequence(start, lastSpace).toString();	
	}

}
